package com.ffws.H08.service.impl;

import com.ffws.H08.model.po.H08FireData;
import com.ffws.H08.repository.H08FireDataRepository;
import com.ffws.H08.service.H08FireDataService;
import com.ffws.H08.utils.FunctionUtil;
import com.ffws.base.model.RestResponse;
import com.google.gson.Gson;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;
import static com.ffws.H08.system.SystemConstant.*;

/**
 * @Author JC
 * @description TODO
 * @Date 2023-10-9 00:52
 * @Version 1.0
 */
@Service
@Slf4j
public class H08FireDataServiceImpl implements H08FireDataService {

    @Resource
    private MongoTemplate mongoTemplate;

    @Resource
    private H08FireDataRepository h08FireDataRepository;

    @Override
    public RestResponse<List<H08FireData>> getH08DataForMongo() {
        Set<String> collectionNames = mongoTemplate.getCollectionNames();
        Object[] objects = collectionNames.toArray();
        Arrays.sort(objects, new Comparator<Object>() {
            @Override
            public int compare(Object o1, Object o2) {
                String s1 = String.valueOf(o1);
                String s2 = String.valueOf(o2);
                return (int) (Long.parseLong(s2.substring(0,s2.indexOf(MONGODB_H08_FIRE_DATA_COLLECTION_SUFFIX))) - Long.parseLong(s1.substring(0,s1.indexOf(MONGODB_H08_FIRE_DATA_COLLECTION_SUFFIX))));
            }
        });
        MongoCollection<Document> collection = mongoTemplate.getCollection(objects[0].toString());
        return RestResponse.success(FunctionUtil.makeMongoH08DocToList(collection.find()));
    }

}
